package com.greensam.common.mq;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 封装接口返回结果
 * @Author: Machengrui
 * @Date: 2024/9/23 21:16
 */
@Data
@ApiModel(value = "通用返回结果")
public class CommonResult<T> {

    @ApiModelProperty(value = "返回码")
    private long code;

    @ApiModelProperty(value = "返回描述")
    private String message;

    @ApiModelProperty(value = "返回对象")
    private T data;

    @ApiModelProperty(value = "返回结果")
    private boolean result;

    protected CommonResult() {
    }

    protected CommonResult(long code, String message, T data, boolean result) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.result = result;
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResult<T> success() {
        return success(ResultCode.SUCCESS);
    }

    /**
     * 成功返回结果
     *
     * @param resultCode 结果码
     */
    public static <T> CommonResult<T> success(ResultCode resultCode) {
        return new CommonResult<T>(resultCode.getCode(), resultCode.getPropertyKey(), null, true);
    }

    /**
     * 成功返回结果
     *
     * @param data 返回数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getPropertyKey(), data, true);
    }

    /**
     * 成功返回结果
     *
     * @param data    返回数据
     * @param massage 返回描述
     */
    public static <T> CommonResult<T> success(T data, String massage) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), massage, data, true);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 失败返回结果
     *
     * @param resultCode 结果码
     */
    public static <T> CommonResult<T> failed(ResultCode resultCode) {
        return new CommonResult<T>(resultCode.getCode(), resultCode.getMessage(), null, false);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(ErrorCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null, false);
    }

    /**
     * 失败返回结果
     *
     * @param message 失败描述
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<>(ResultCode.FAILED.getCode(), message, null, false);
    }

    /**
     * 失败返回结果
     *
     * @param message 失败描述
     * @param data 数据
     */
    public static <T> CommonResult<T> failed(String message, T data) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, data, false);
    }

    /**
     * 失败返回结果
     *
     * @param code    返回码
     * @param message 返回描述
     */
    public static <T> CommonResult<T> failed(Long code, String message) {
        return new CommonResult<T>(code, message, null, false);
    }

    /**
     * 未登录返回结果
     *
     * @param data 数据
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getPropertyKey(), data, false);
    }

    /**
     * 未授权返回结果
     *
     * @param data 数据
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getPropertyKey(), data, false);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 返回描述
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null, false);
    }
}
